home *** CD-ROM | disk | FTP | other *** search
- /*
- DUFTP
- */
-
- // LINKS to DIP for dialup access
-
- // This module is a little obscure - sorry, but it has to be that way to
- // allow the DIP program to be monitored without recompiling it and
- // still get some GEM feedback on whats happening.
- //
- // Major obscure bits are the fork() (ok, Unix heads have no problem here)
- // and the use of Set_timer_callback() - this is a DULIB function that
- // effectively multithreads a GEM program by calling a callback at regular
- // intervals (automatically), so I can poll for the DIP program dieing
- // and open and close dialogs as a result - needed 'coz you cann't call
- // GEM from inside a signal handler.
-
- #include <DULIB.H>
- #include <UNISTD.H>
- #include <SIGNAL.H>
- #include <FCNTL.H>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <net/if.h>
- #include <sockios.h>
- #include "duftp_n.h"
- #include "globals.h"
-
- int dip_o_file;
- short dip_online=0;
- short dip_running=0;
- __Sigansi original_SIGCHLD;
-
- // Flag that we've noticed that DIP has died, so that the GEM based timer
- // callback will have a bit of a snoop to see if SLIP is up yet.
- void dip_died(int a)
- {
- signal(SIGCHLD,SIG_IGN);
- dip_running=2;
- }
-
- short dip_close_connected_dialog(void)
- {
- close_dialog(Dip_connected); //Close the connected dialog
- Set_timer_callback(0,NULL); //Clear the timer callback, we don't need it anymore
- return TRUE;
- }
-
- // detect whether the slip link actually came up or not
- short dip_detect_connect(void)
- {
- int sock;
- struct ifreq ifr;
-
- sock = socket (PF_INET, SOCK_DGRAM, 0);
- if (sock>0)
- {
- strcpy (ifr.ifr_name, "sl0");
- if (ioctl (sock, SIOCGIFFLAGS, &ifr) < 0) {
- form_alert(1,"[3][ SLIP: sl0 | Cannot get FLAGS. ][ ok ]");
- }else{
- if (ifr.ifr_flags&IFF_UP) // Did DIP do the job?
- {
- activate_dialog(Dip_connected,"Connected", DIAL_NO_CLOSE); // Open a dialog to say we're connected
- Set_timer_callback(4000,&dip_close_connected_dialog); // Set a timed callback to close the connected dialog for us
- dip_online=1; // Flag slip link as being up
- }else{
- Set_timer_callback(0,NULL); //Clear the timer callback, we don't need it anymore
- dip_running=0;
- dip_online=0;
- }
- }
- close(sock);
- }
-
- return TRUE;
- }
-
- // Snoop the dip_running signal to see if DIP is actually still going, or if
- // it is dead. A DULIB pseudo multitasked routine, we can actually use GEM
- // commands here, so we can close the DIP dialog.
- short dip_detect_dip_die(void)
- {
- if (dip_running<2) return FALSE; // Is DIP still alive? 0=not running, 1=running, 2=dieing/dead
- // If dieing/dead, then start the check for slip link (sl0) sequence
- close_dialog(Dip_dialing); //Close the dialing dialog
- Set_timer_callback(4000,&dip_detect_connect); // Set a timed callback to check if DIP managed to enable sl0
- // (give it 4 seconds for the link to be up & stable)
- return TRUE;
- }
-
- short dip_dialup(void)
- {
- char d[FMSIZE];
-
- if (dip_online) return TRUE;
-
- if (form_alert(1,"[2][ DIAL-UP INTERNET PROTOCOL | Open a SLIP connection ? ][ Yes | No ]")==2) return TRUE;
-
- // Change to the extra's directory where I've put DIP
- getcwd(d,FMSIZE);
- chdir(initial_dir);
- chdir("extras");
-
- signal(SIGCHLD,dip_died);
- dip_online=1;
- dip_running=1;
-
- Set_timer_callback(1000,&dip_detect_dip_die);
-
- // Spawn DIP
- activate_dialog(Dip_dialing,"DIP DIALUP",DIAL_NO_CLOSE);
-
- dip_o_file=creat("dip.dbg",O_WRONLY);
-
- if (fork()==0)
- {
- dup2(dip_o_file, 1);
- dup2(dip_o_file, 2);
- execl("dip.ttp","dip.ttp","slip.dip",NULL);
- exit(0);
- }
-
- // Restore the current directory
- chdir(d);
-
- return TRUE;
- }
-
- short dip_hangup(void)
- {
- char d[FMSIZE];
-
- if (!dip_online) return TRUE;
-
- if (form_alert(1,"[2][ DIAL-UP INTERNET PROTOCOL | Close SLIP connection ? ][ Yes | No ]")==2) return TRUE;
-
- // Change to the extra's directory where I've put DIP
- getcwd(d,FMSIZE);
- chdir(initial_dir);
- chdir("extras");
-
- // Spawn DIP
- signal(SIGCHLD,original_SIGCHLD);
- if (fork()==0)
- {
- dup2(dip_o_file, 1);
- dup2(dip_o_file, 2);
- execl("dip.ttp","dip,ttp","-k",NULL);
- }
- wait(0);
- close(dip_o_file);
-
- signal(SIGCHLD,SIG_IGN);
-
- // Restore the current directory
- chdir(d);
-
- dip_online=0;
-
- return TRUE;
- }
-